﻿using System;
using System.Data;
using TopBuilds;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrintTemplTextObject
    {
        public static PrintTemplText[] GetPrintTemplTexts()
        {
            return Query(null);
        }

        public static PrintTemplText GetPrintTemplText(int id)
        {
            PrintTemplText printTemplText = new PrintTemplText();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintTemplTextDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintTemplTextDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable printTemplTexts = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            printTemplText.Id                   = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.Id]);
            printTemplText.TemplateId           = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.TemplateId]);
            printTemplText.Font                 = Convert.ToString(printTemplTexts.Rows[0][PrintTemplTextDesc.Font]);
            printTemplText.FontSize             = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.FontSize]);
            printTemplText.X                    = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.X]);
            printTemplText.Y                    = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.Y]);
            printTemplText.Text                 = Convert.ToString(printTemplTexts.Rows[0][PrintTemplTextDesc.Text]);
            printTemplText.Width                = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.Width]);
            printTemplText.Height               = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.Height]);
            printTemplText.TextType             = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.TextType]);
            printTemplText.Portion              = Convert.ToInt32(printTemplTexts.Rows[0][PrintTemplTextDesc.Portion]);


            return printTemplText;
        }

        public static PrintTemplText[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + PrintTemplateDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data

            PrintTemplText[] printTemplTexts = new PrintTemplText[table.Rows.Count];
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    PrintTemplText printTemplText = new PrintTemplText();

                    printTemplText.Id                     = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.Id]);
                    printTemplText.TemplateId             = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.TemplateId]);
                    printTemplText.Font                   = Convert.ToString(table.Rows[i][PrintTemplTextDesc.Font]);
                    printTemplText.FontSize               = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.FontSize]);
                    printTemplText.X                      = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.X]);
                    printTemplText.Y                      = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.Y]);
                    printTemplText.Text                   = Convert.ToString(table.Rows[i][PrintTemplTextDesc.Text]);
                    printTemplText.Width                  = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.Width]);
                    printTemplText.Height                 = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.Height]);
                    printTemplText.TextType               = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.TextType]);
                    printTemplText.Portion                = Convert.ToInt32(table.Rows[i][PrintTemplTextDesc.Portion]);

                    printTemplTexts.SetValue(printTemplText, i);
                }

                return printTemplTexts;
            }
            return null;
        }

        public static Int32 Add(PrintTemplText printTemplText)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrintTemplTextDesc.TemplateId,         printTemplText.TemplateId);
            builder.InsertAdd(PrintTemplTextDesc.Font,               printTemplText.Font);
            builder.InsertAdd(PrintTemplTextDesc.FontSize,           printTemplText.FontSize);
            builder.InsertAdd(PrintTemplTextDesc.X,                  printTemplText.X);
            builder.InsertAdd(PrintTemplTextDesc.Y,                  printTemplText.Y);
            builder.InsertAdd(PrintTemplTextDesc.Text,               printTemplText.Text);
            builder.InsertAdd(PrintTemplTextDesc.Width,              printTemplText.Width);
            builder.InsertAdd(PrintTemplTextDesc.Height,             printTemplText.Height);
            builder.InsertAdd(PrintTemplTextDesc.TextType,           printTemplText.TextType);
            builder.InsertAdd(PrintTemplTextDesc.Portion,            printTemplText.Portion);


            builder.QueryTableName = PrintTemplTextDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintTemplTextDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintTemplTextDesc.TableName;
            string sql = builder.InsertCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(PrintTemplText printTemplText)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrintTemplTextDesc.TemplateId,             printTemplText.TemplateId);
            builder.UpdateAdd(PrintTemplTextDesc.Font,                   printTemplText.Font);
            builder.UpdateAdd(PrintTemplTextDesc.FontSize,               printTemplText.FontSize);
            builder.UpdateAdd(PrintTemplTextDesc.X,                      printTemplText.X);
            builder.UpdateAdd(PrintTemplTextDesc.Y,                      printTemplText.Y);
            builder.UpdateAdd(PrintTemplTextDesc.Text,                   printTemplText.Text);
            builder.UpdateAdd(PrintTemplTextDesc.Width,                  printTemplText.Width);
            builder.UpdateAdd(PrintTemplTextDesc.Height,                 printTemplText.Height);
            builder.UpdateAdd(PrintTemplTextDesc.TextType,               printTemplText.TextType);
            builder.UpdateAdd(PrintTemplTextDesc.Portion,                printTemplText.Portion);
            builder.ConditionAdd(PrintTemplTextDesc.Id, ConditionExpress.EQUALS, printTemplText.Id);
            builder.QueryTableName = PrintTemplTextDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

    }
}
